package com.itheima;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.domain.Address;
import com.itheima.domain.User;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class Servlet implements Converter<String, List> {

    @RequestMapping("/save")
    public String finAll(User user){
        System.out.println(user);
        return "forward:int.jsp";
    }

    @RequestMapping("/date")

    public String dateAll(@DateTimeFormat(pattern = "yyyy-MM-dd")Date date){
        System.out.println(date);
        return "/int.jsp";
    }

    @RequestMapping("/aaa")
    @ResponseBody
    public ModelAndView pageDemo1(ModelAndView modelAndView){
        User user = new User();
        user.setName("Spring响应案例一");
        user.setAge(15);

        modelAndView.addObject("abc",user);

        modelAndView.addObject("name","你好");
        modelAndView.setViewName("/int.jsp");
        return modelAndView;
    }


    @RequestMapping("/bbb")
    @ResponseBody
    public User pageDemo1() throws JsonProcessingException {
        User user = new User();
        user.setName("Spring响应案例一");
        user.setAge(15);

//        ObjectMapper objectMapper = new ObjectMapper();
//        String s = objectMapper.writeValueAsString(user);
        return user;
    }

    @Override
    public List convert(String source) {
        String[] split = source.split("-");
        List<String> abc=new ArrayList();
        for (String s : split) {
            abc.add(s);
        }
        return abc;
    }
}
